package com.dyz.wsw.common.utils.security;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 主要用于校验文件完整性
 * @author dyz
 * Time 2024/7/6 3:29
 */
public class MD5 {

    /**
     * 将字串通过md5转成校验用hash
     * @param input 要转换的串
     * @return md5后生成的值
     * @throws NoSuchAlgorithmException 没有对应的算法
     */
    public static String encrypt(String input){
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        byte[] digest = md5.digest(input.getBytes(StandardCharsets.UTF_8));
        // 将字节数组转换为16进制字符串
        StringBuilder sb = new StringBuilder();

        for (byte b : digest) {
            sb.append(String.format("%02x", b & 0xff));
        }
        return sb.toString();
    }
}
